package leetcode.code2006;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

//执行用时：1 ms, 在所有 Java 提交中击败了84.13%的用户
//
//内存消耗：40.7 MB, 在所有 Java 提交中击败了9.81%的用户
//
//通过测试用例：237 / 237
public class Solution2 implements IDebug {

	public int countKDifference(int[] nums, int k) {
		int len = nums.length;
		int ans = 0;
		int[] hash = new int[101];
		for (int i = 0; i < len; i++) {
			int cur = nums[i];
			int find1 = cur + k;
			ans += find1 >= 0 && find1 < 101 ? hash[find1] : 0;// 注意范围，别忘大端
			int find2 = cur - k;
			ans += find2 >= 0 && find2 < 101 ? hash[find2] : 0;
			hash[cur] += 1;
		}
		return ans;
	}

	@Override
	public void debug1() {
		HelpDebug.compare(11, this.countKDifference(HelpDebug.str2array3(
				"[33,90,62,43,21,96,20,18,84,74,61,100,5,11,4,67,96,18,6,68,82,32,76,33,93,33,71,32,30,63,37,46,95,51,63,77,63,84,52,78,66,76,66,9,73,92,79,65,29,42]"),
				64));
	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution2 so = new Solution2();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
